/*
 * 路由 
 */
import {
  nextTick
} from 'vue';
import nProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {
  createRouter,
  createWebHashHistory
} from 'vue-router';
import {
  routerArray
} from './routers';
import {
  KEY_CONST
} from '@/constants/system-const';
import {
  useUserStore
} from '@/store/modules/user';
import _ from 'lodash';
import menu from '@/assets/menu';


export const router = createRouter({
  history: createWebHashHistory(),
  routes: routerArray,
  strict: true,
  scrollBehavior: () => ({
    left: 0,
    top: 0
  }),
});

// ----------------------- 路由加载前 -----------------------
router.beforeEach(async (to, from, next) => {
  // 进度条开启
  nProgress.start();
  // 公共页面，任何时候都可以跳转
  if (to.path === KEY_CONST.PAGE_PATH_404) {
    next();
    return;
  }
  // 跳转到登录页面
  if (to.path === KEY_CONST.PAGE_PATH_LOGIN) {
    useUserStore().logout();
    next();
    return;
  }
  // 登录页，则跳转到首页
  if (to.path === KEY_CONST.PAGE_PATH_LOGIN) {
    next({
      path: KEY_CONST.HOME_PAGE_NAME
    });
    return;
  }

  // 下载路由对应的 页面组件，并修改组件的Name，如果修改过，则不需要修改
  let toRouterInfo = routerMap.get(to.name);
  if (toRouterInfo && _.isFunction(toRouterInfo.component) && toRouterInfo.meta.renameComponentFlag === false) {
    // 因为组件component 为 lazy load是个方法，所以可以直接执行 component()方法
    toRouterInfo.component().then((val) => {
      // 修改组件的name
      val.default.name = to.meta.componentName;
      // 记录已经修改过 组件的name
      toRouterInfo.meta.renameComponentFlag = true;
    });
  }
  // 设置tagNav
  useUserStore().setTagNav(to, from);
  // 设置keepAlive
  if (to.meta.keepAlive) {
    nextTick(() => {
      useUserStore().pushKeepAliveIncludes(to.meta.componentName);
    });
  }
  next();
});

// ----------------------- 路由加载后 -----------------------
router.afterEach(() => {
  nProgress.done();
});

// ----------------------- 构建router对象 -----------------------
const routerMap = new Map();

export function buildRoutes(menuRouterList) {
  const modules =
    import.meta.glob('../views/**/**.vue');
  // let menuList = menuRouterList ? menuRouterList : useUserStore().getMenuRouterList || [];
  let menuList = menu;
  /**
   * 1、构建整个路由信息
   * 2、添加到路由里
   */
  const routerList = [];
  // 获取所有vue组件 用于注入name属性 name属性用于keep-alive
  //1、构建整个路由信息
  for (const e of menuList) {
    if (!e.menuId) {
      continue;
    }
    if (!e.path) {
      continue;
    }
    if (e.deletedFlag && e.deletedFlag === true) {
      continue;
    }
    let route = {
      path: e.path.startsWith('/') ? e.path : `/${e.path}`,
      // 使用【menuId】作为name唯一标识
      name: e.menuId.toString(),
      props: e.props,
      meta: {
        // 数据库菜单(页面)id
        id: e.menuId.toString(),
        // 组件名称
        componentName: e.menuId.toString(),
        // 菜单展示
        title: e.menuName,
        // 菜单图标展示
        icon: e.icon,
        // 是否在菜单隐藏
        hideInMenu: !e.visibleFlag,
        // 页面是否keep-alive缓存
        keepAlive: e.cacheFlag,
        // 是否为外链
        frameFlag: e.frameFlag,
        // 外链地址
        frameUrl: e.frameUrl,
        // 是否 rename了组件的名字
        renameComponentFlag: false,
      },
    };
    if (e.frameFlag) {
      route.component = () => import('@/layout/iframe-index.vue');
    } else {
      let componentPath = e.component && e.component.startsWith('/') ? e.component : '/' + e.component;
      let relativePath = `../views${componentPath}`;
      route.component = modules[relativePath];
    }
    routerList.push(route);
    routerMap.set(e.menuId.toString(), route);
  }

  //2、添加到路由里
  router.addRoute({
    path: '/work',
    meta: {},
    component: TopLeftCenter,
    children: routerList,
  });
}